package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProfileDetailMapper;
import com.indusfo.spc.mapper.ProfileMapper;
import com.indusfo.spc.pojo.Profile;
import com.indusfo.spc.pojo.ProfileDetail;
import com.indusfo.spc.service.ProfileService;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.untlis.UuidUtil;
import com.indusfo.spc.vo.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author chenm
 * @create 2019-11-26 9:40
 */
@Service
public class ProfileServiceImp implements ProfileService {

  @Resource
  private ProfileMapper profileMapper;

  @Resource
  private ProfileDetailMapper profileDetailMapper;

  @Override
  public JSONObject selectProfile(Profile profile) {
    try {
      // 获取一页显示多少行
      Integer pagesize = profile.getPagesize();
      // 获取查询第几页
      Integer pageindex = profile.getPageindex();
      // 若不为空计算从第几条开始查询
      if (pagesize != null && pageindex != null) {
        profile.setIncept(pagesize * (pageindex - 1));
      }
      List<Profile> profiles = profileMapper.selectProfile(profile);
      if(profiles.isEmpty()){
        return JSONObject.oK("没有查询到数据", profiles, 0);
      }else{
        int count  = profileMapper.selectCount(profile);
        return JSONObject.oK( profiles, count);
      }
    }catch (Exception  e){
      e.printStackTrace();
      return JSONObject.build(JSONObject.ERROR, e.getMessage());
    }
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public JSONObject saveProfile(Profile profile) {
    /*
      根据产品和编码查询，工艺文件库中是否已经存在
        */
      String uuid = UuidUtil.getUuid();
      Integer profileId;
      List<Profile> profiles = profileMapper.checkHasProfile(profile);
      if(profiles.isEmpty()){
        profile.setUuid(uuid);
        profileMapper.insertProfile(profile);
        profileId =profileMapper.selectByUuid(uuid).getProfileId();
      }else{
        profile.setProfileId(profiles.get(0).getProfileId());
        profileMapper.updateProfile(profile);
        profileId = profiles.get(0).getProfileId();
      }

      Date date = new Date();
      /**
       * 页面传回的文件集合
      */
      List<ProfileDetail> htmlDetails = profile.getDetails();
      /**
       * 根据产品和版本查询到的文件集合
      */
      List<ProfileDetail> dataDetails = profileDetailMapper.selectByMatId(profile.getMatId());
      /**
       * 差集，页面有的 数据库没有
       */
      List<ProfileDetail> addDetails = htmlDetails.stream().filter(item -> !dataDetails.stream().map(e -> e.getProfileDetailId()).collect(Collectors.toList()).contains(item.getProfileDetailId())).collect(Collectors.toList());

      /**
       * 差集，页面没有的 数据库有
       */
      List<ProfileDetail> delDetails = dataDetails.stream().filter(item -> !htmlDetails.stream().map(e -> e.getProfileDetailId()).collect(Collectors.toList()).contains(item.getProfileDetailId())).collect(Collectors.toList());

      /**
       * 交集
       */
      List<ProfileDetail> updeteDetails = htmlDetails.stream().filter(item ->dataDetails.stream().map(e -> e.getProfileDetailId()).collect(Collectors.toList()).contains(item.getProfileDetailId())).collect(Collectors.toList());
     

    if(!addDetails.isEmpty()){
      for(ProfileDetail detail : addDetails){
        if(StringUtils .isBlank(detail.getUuid())){
          detail.setUuid(UuidUtil.getUuid());
        }else{
          detail.setCreateDate(date);
          detail.setModifyDate(date);
        }
        detail.setProfileId(profileId);
        detail.setMatId(profile.getMatId());
        detail.setMatVerId(profile.getMatVerId());
      }
      profileDetailMapper.addDetails(addDetails);
    }

    if(!delDetails.isEmpty()){
      List<String> uuidList=delDetails.stream().map(ProfileDetail::getUuid).collect(Collectors.toList());
      profileDetailMapper.delDetailsByUuid(uuidList);
    }
    
    if(!updeteDetails.isEmpty()){
      for(ProfileDetail detail : updeteDetails){
        detail.setModifyDate(date);
        detail.setProfileId(profile.getProfileId());
        detail.setMatId(profile.getMatId());
        detail.setMatVerId(profile.getMatVerId());
      }
      profileDetailMapper.updateDetails(updeteDetails);
    }
    
      return JSONObject.build(JSONObject.SUCCESS, "保存成功");
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public JSONObject deleteProfile(List<Integer> profileIds, Integer dataState) {
    profileMapper.deleteProfile(profileIds,dataState);
    // 根据返回的影响行数判断是否成功
    String msg = "";
    switch (dataState) {
      case 1:
        msg = "启用";
        break;
      case 2:
        msg = "删除";
        profileDetailMapper.delProfileDetailByProfileIds(profileIds);
        break;
      case 3:
        msg = "停用";
        break;
      default:
        msg = "";
    }
    return JSONObject.oK(msg + "成功", JSONObject.SUCCESS);
  }

  @Override
  public String checkNoRepeatExcel(Profile profile) {
    return null;
  }

  @Override
  public JSONObject updateListPallet(List<Profile> profileList) {
    return null;
  }

}
